import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import utils from 'server/core/utils';
import itemModel from 'server/model/itemModel';
let itemsConfig = totalConfig.item;
let equipmentsConfig = totalConfig.equipment;
let weaponTypes = []; //武器池
let equipmentTypes = [] //装备池
for (let [id, value] of Object.entries(equipmentsConfig)) {
    if (value.type === 1) {
        weaponTypes.push(id);
    } else {
        equipmentTypes.push(id);
    }
}

class EquipmentHandle {
    static getInstance() {
        if (!EquipmentHandle.instance) {
            EquipmentHandle.instance = new EquipmentHandle();
        }
        return EquipmentHandle.instance;
    }

    create(lvl) {
        let equipmentId;
        //判断出什么装备
        if (Math.random() * 100 < 20) { //几率出武器
            equipmentId = utils.getOneFromArray(weaponTypes);
        } else { //几率出防具
            equipmentId = utils.getOneFromArray(equipmentTypes);
        }


        //创建装备主属性
        let mainAttri = {};
        let mainAttriConfig = equipmentsConfig[equipmentId].mainAttri;
        for (let [attri, value] of Object.entries(mainAttriConfig)) {
            mainAttri[attri] = utils.addRandomHalf2(value[0] * lvl + value[1])
        }

        let equipment = itemModel.create({
            templateId: equipmentId,
            mainAttri: mainAttri
        })
        return equipment;
    }

    createByEquipmentId(equipmentId) {
        let equipment = itemModel.create({
            templateId: equipmentId,
        })
        return equipment;
    }
}

export default EquipmentHandle.getInstance();